const express = require('express')

const dbop = require('./../mod/crud.js')

var app = express.Router()

// 登录
app.get('/', (req, res) => {
	res.redirect('/login')
})
app.get('/login', (req, res) => {
	res.send('damn')
})
app.post('/login', (req, res) => {
	let { username : usrname, password : passwd } = req.body
	if (usrname != 'admin' || passwd != '123456') {
		return res.status(200).send({ status: '400' })
	}
	res.status(200).send({ status: '200', token: 'wsxkcialoqlMxZm' })	
})

// 侧边栏
app.get('/menus', (req, res) => {
	dbop.find('roledata', (err, info) => {
		if (err) {
			return res.status(200).send({ status: '400', msg: err })
		}
		const ret = {
			data: info,
			status: '200'
		}
		res.status(200).send(ret)
	})
})

// 用户管理
// 1、根据id获得一位user
app.get('/fetch', (req, res) => {
	const id = req.query.id - 0
	dbop.findById('userlist', id, (err, info) => {
		if (err) {
			return res.status(200).send({ status: '400', msg: err })
		}
		const ret = {
			data: info,
			status: '200'
		}
		res.status(200).send(ret)
	})
})

// 2、根据id修改user
app.get('/edit', (req, res) => {
	let { id, tel } = req.query
	dbop.update('userlist', id, { tel }, (err, info) => {
		if (err) {
			return res.status(200).send({ status: '400', msg: err })
		}
		res.status(200).send({ status: '200', data: info })
	})
})

// 3、根据id修改role_status
app.get('/userstate', (req, res) => {
	let { id, role_status } = req.query
	dbop.update('userlist', id, { role_status: role_status == 'true' }, (err, info) => {
		if (err) {
			return res.status(200).send({ status: '400', msg: err })
		}
		res.status(200).send({ status: '200', data: info })
	})
})

// 4、获取用户列表，按照前端的页面大小，页码及query字段匹配！
app.get('/users', (req, res) => {
	dbop.findWithFileter('userlist', req.query, (err, data, obj) => {
		if (err) {
			// console.log(err)
			return res.status(200).send({ status: '400', msg: err })
		}
		const retobj = {
			data: {
				pagenum: obj.pagenum,
				total: obj.total,
				users: data
			},
			status: '200'
		}
		res.status(200).send(retobj)
	})
})

// 5、添加用户
app.post('/adduser', (req, res) => {
	console.log(req.body)
	dbop.adduser(req.body, (err, newuser) => {
		if (err) {
			console.log(err)
			return res.status(200).send({ status: '400', msg: err })
		}
		res.status(200).send({ data: newuser, status: '200'})
	})
})


// 6、删除用户
app.get('/deluser', (req, res) => {
	const id = req.query.id - 0
	dbop.dropById(id, (err) => {
		if (err) {
			return res.status(200).send({ data: null, status: '500', msg: 'failed!'})
		}
		res.status(200).send({ data: null, status: '200', msg: 'done!' })
	})
})




// 权限管理
app.get('/rightsList', (req, res) => {
	// // 这里莫名其妙拿不到数据
	// dbop.find('rightsList', (err, info) => {
	// 	console.log('你想拿到权限')
	// 	if (err) {
	// 		return res.status(200).send({ status: '400', msg: err.toString() })
	// 	}
	// 	const ret = {
	// 		data: info,
	// 		status: '200'
	// 	}
	// 	res.status(200).send(ret)
	// })
	const info = [
		{
			id: 101,
			authName: '商品管理',
			level: 0,
			pid: 0,
			path: 'goods'
		},
		{
			id: 102,
			authName: '订单管理',
			level: 0,
			pid: 0,
			path: 'orders'
		},
		{
			id: 103,
			authName: '权限管理',
			level: 0,
			pid: 0,
			path: 'rights'
		},
		{
			id: 113,
			authName: '商品列表',
			level: 1,
			pid: 0,
			path: 'goods'
		}
	]

	const ret = {
		data: info,
		status: '200'
	}
	res.status(200).send(ret)
})

app.get('/roles', (q, res) => {
	const info = [
		{ id: 22, roleName: '主管', roleDesc: '技术负责人', children: [
			{ id: 101, authName: '商品管理', level: 0, path: 'goods', children: [
				{ id: 113, authName: "商品列表", path: 'goodslist', level: 1 },
				{ id: 114, authName: "商品列表2", path: 'goodslist2', level: 1 },
				{ id: 115, authName: "商品列表3", path: 'goodslist3', level: 1 },
				{ id: 116, authName: "商品列表4", path: 'goodslist4', level: 1 }
			] },
			{ id: 201, authName: '订单管理', level: 0, path: 'orders', children: [
				{ id: 213, authName: "订单列表", path: 'orderslist', level: 1 },
				{ id: 214, authName: "订单列表2", path: 'orderslist2', level: 1 },
				{ id: 215, authName: "订单列表3", path: 'orderslist3', level: 1 },
				{ id: 216, authName: "订单列表4", path: 'orderslist4', level: 1 }
			] }
		] },
		{ id: 23, roleName: '某某某', roleDesc: '资深码农', children: [
			{ id: 101, authName: '商品管理', level: 0, path: 'goods', children: [
				{ id: 113, authName: "商品列表", path: 'goodslist', level: 1 },
				{ id: 114, authName: "商品列表2", path: 'goodslist2', level: 1 },
				{ id: 115, authName: "商品列表3", path: 'goodslist3', level: 1 },
				{ id: 116, authName: "商品列表4", path: 'goodslist4', level: 1 }
			] },
		] }
	]
	const ret = {
		data: info,
		status: '200'
	}
	res.status(200).send(ret)
})

module.exports = app
